<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Form and field validation &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../index.html" />
    <link rel="up" title="Forms" href="index.html" />
    <link rel="next" title="Middleware" href="../middleware.html" />
    <link rel="prev" title="Widgets" href="widgets.html" />



 
<script type="text/javascript" src="../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../index.html">Home</a>  |
        <a title="Table of contents" href="../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../genindex.html">Index</a>  |
        <a title="Module index" href="../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="widgets.html" title="Widgets">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../middleware.html" title="Middleware">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-forms-validation">
            
  <div class="section" id="s-form-and-field-validation">
<span id="s-id1"></span><span id="form-and-field-validation"></span><span id="id1"></span><h1>Form and field validation<a class="headerlink" href="#form-and-field-validation" title="Permalink to this headline">¶</a></h1>
<p>Form validation happens when the data is cleaned. If you want to customize
this process, there are various places you can change, each one serving a
different purpose. Three types of cleaning methods are run during form
processing. These are normally executed when you call the <tt class="docutils literal"><span class="pre">is_valid()</span></tt>
method on a form. There are other things that can trigger cleaning and
validation (accessing the <tt class="docutils literal"><span class="pre">errors</span></tt> attribute or calling <tt class="docutils literal"><span class="pre">full_clean()</span></tt>
directly), but normally they won&#8217;t be needed.</p>
<p>In general, any cleaning method can raise <tt class="docutils literal"><span class="pre">ValidationError</span></tt> if there is a
problem with the data it is processing, passing the relevant information to
the <tt class="docutils literal"><span class="pre">ValidationError</span></tt> constructor. <a class="reference internal" href="#raising-validation-error"><em>See below</em></a>
for the best practice in raising <tt class="docutils literal"><span class="pre">ValidationError</span></tt>. If no <tt class="docutils literal"><span class="pre">ValidationError</span></tt>
is raised, the method should return the cleaned (normalized) data as a Python
object.</p>
<p>Most validation can be done using <a class="reference internal" href="#validators">validators</a> - simple helpers that can be
reused easily. Validators are simple functions (or callables) that take a single
argument and raise <tt class="docutils literal"><span class="pre">ValidationError</span></tt> on invalid input. Validators are run
after the field&#8217;s <tt class="docutils literal"><span class="pre">to_python</span></tt> and <tt class="docutils literal"><span class="pre">validate</span></tt> methods have been called.</p>
<p>Validation of a Form is split into several steps, which can be customized or
overridden:</p>
<ul>
<li><p class="first">The <tt class="docutils literal"><span class="pre">to_python()</span></tt> method on a Field is the first step in every
validation. It coerces the value to correct datatype and raises
<tt class="docutils literal"><span class="pre">ValidationError</span></tt> if that is not possible. This method accepts the raw
value from the widget and returns the converted value. For example, a
FloatField will turn the data into a Python <tt class="docutils literal"><span class="pre">float</span></tt> or raise a
<tt class="docutils literal"><span class="pre">ValidationError</span></tt>.</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">validate()</span></tt> method on a Field handles field-specific validation
that is not suitable for a validator. It takes a value that has been
coerced to correct datatype and raises <tt class="docutils literal"><span class="pre">ValidationError</span></tt> on any error.
This method does not return anything and shouldn&#8217;t alter the value. You
should override it to handle validation logic that you can&#8217;t or don&#8217;t
want to put in a validator.</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">run_validators()</span></tt> method on a Field runs all of the field&#8217;s
validators and aggregates all the errors into a single
<tt class="docutils literal"><span class="pre">ValidationError</span></tt>. You shouldn&#8217;t need to override this method.</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">clean()</span></tt> method on a Field subclass. This is responsible for
running <tt class="docutils literal"><span class="pre">to_python</span></tt>, <tt class="docutils literal"><span class="pre">validate</span></tt> and <tt class="docutils literal"><span class="pre">run_validators</span></tt> in the correct
order and propagating their errors. If, at any time, any of the methods
raise <tt class="docutils literal"><span class="pre">ValidationError</span></tt>, the validation stops and that error is raised.
This method returns the clean data, which is then inserted into the
<tt class="docutils literal"><span class="pre">cleaned_data</span></tt> dictionary of the form.</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">clean_&lt;fieldname&gt;()</span></tt> method in a form subclass &#8211; where
<tt class="docutils literal"><span class="pre">&lt;fieldname&gt;</span></tt> is replaced with the name of the form field attribute.
This method does any cleaning that is specific to that particular
attribute, unrelated to the type of field that it is. This method is not
passed any parameters. You will need to look up the value of the field
in <tt class="docutils literal"><span class="pre">self.cleaned_data</span></tt> and remember that it will be a Python object
at this point, not the original string submitted in the form (it will be
in <tt class="docutils literal"><span class="pre">cleaned_data</span></tt> because the general field <tt class="docutils literal"><span class="pre">clean()</span></tt> method, above,
has already cleaned the data once).</p>
<p>For example, if you wanted to validate that the contents of a
<tt class="docutils literal"><span class="pre">CharField</span></tt> called <tt class="docutils literal"><span class="pre">serialnumber</span></tt> was unique,
<tt class="docutils literal"><span class="pre">clean_serialnumber()</span></tt> would be the right place to do this. You don&#8217;t
need a specific field (it&#8217;s just a <tt class="docutils literal"><span class="pre">CharField</span></tt>), but you want a
formfield-specific piece of validation and, possibly,
cleaning/normalizing the data.</p>
<p>This method should return the cleaned value obtained from cleaned_data,
regardless of whether it changed anything or not.</p>
</li>
<li><p class="first">The Form subclass&#8217;s <tt class="docutils literal"><span class="pre">clean()</span></tt> method. This method can perform
any validation that requires access to multiple fields from the form at
once. This is where you might put in things to check that if field <tt class="docutils literal"><span class="pre">A</span></tt>
is supplied, field <tt class="docutils literal"><span class="pre">B</span></tt> must contain a valid email address and the
like. This method can return a completely different dictionary if it wishes,
which will be used as the <tt class="docutils literal"><span class="pre">cleaned_data</span></tt>.</p>
<p>Since the field validation methods have been run by the time <tt class="docutils literal"><span class="pre">clean()</span></tt> is
called, you also have access to the form&#8217;s <tt class="docutils literal"><span class="pre">errors</span></tt> attribute which
contains all the errors raised by cleaning of individual fields.</p>
<p>Note that any errors raised by your <a class="reference internal" href="api.html#django.forms.Form.clean" title="django.forms.Form.clean"><tt class="xref py py-meth docutils literal"><span class="pre">Form.clean()</span></tt></a> override will not
be associated with any field in particular. They go into a special
&#8220;field&#8221; (called <tt class="docutils literal"><span class="pre">__all__</span></tt>), which you can access via the
<a class="reference internal" href="api.html#django.forms.Form.non_field_errors" title="django.forms.Form.non_field_errors"><tt class="xref py py-meth docutils literal"><span class="pre">non_field_errors()</span></tt></a> method if you need to. If you
want to attach errors to a specific field in the form, you need to call
<a class="reference internal" href="api.html#django.forms.Form.add_error" title="django.forms.Form.add_error"><tt class="xref py py-meth docutils literal"><span class="pre">add_error()</span></tt></a>.</p>
<p>Also note that there are special considerations when overriding
the <tt class="docutils literal"><span class="pre">clean()</span></tt> method of a <tt class="docutils literal"><span class="pre">ModelForm</span></tt> subclass. (see the
<a class="reference internal" href="../../topics/forms/modelforms.html#overriding-modelform-clean-method"><em>ModelForm documentation</em></a> for more information)</p>
</li>
</ul>
<p>These methods are run in the order given above, one field at a time.  That is,
for each field in the form (in the order they are declared in the form
definition), the <tt class="docutils literal"><span class="pre">Field.clean()</span></tt> method (or its override) is run, then
<tt class="docutils literal"><span class="pre">clean_&lt;fieldname&gt;()</span></tt>. Finally, once those two methods are run for every
field, the <a class="reference internal" href="api.html#django.forms.Form.clean" title="django.forms.Form.clean"><tt class="xref py py-meth docutils literal"><span class="pre">Form.clean()</span></tt></a> method, or its override, is executed whether
or not the previous methods have raised errors.</p>
<p>Examples of each of these methods are provided below.</p>
<p>As mentioned, any of these methods can raise a <tt class="docutils literal"><span class="pre">ValidationError</span></tt>. For any
field, if the <tt class="docutils literal"><span class="pre">Field.clean()</span></tt> method raises a <tt class="docutils literal"><span class="pre">ValidationError</span></tt>, any
field-specific cleaning method is not called. However, the cleaning methods
for all remaining fields are still executed.</p>
<div class="section" id="s-raising-validationerror">
<span id="s-raising-validation-error"></span><span id="raising-validationerror"></span><span id="raising-validation-error"></span><h2>Raising <tt class="docutils literal"><span class="pre">ValidationError</span></tt><a class="headerlink" href="#raising-validationerror" title="Permalink to this headline">¶</a></h2>
<div class="versionchanged">
<span class="title">Changed in Django 1.6.</span> </div>
<p>In order to make error messages flexible and easy to override, consider the
following guidelines:</p>
<ul>
<li><p class="first">Provide a descriptive error <tt class="docutils literal"><span class="pre">code</span></tt> to the constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Good</span>
<span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s">&#39;invalid&#39;</span><span class="p">)</span>

<span class="c"># Bad</span>
<span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value&#39;</span><span class="p">))</span>
</pre></div>
</div>
</li>
<li><p class="first">Don&#8217;t coerce variables into the message; use placeholders and the <tt class="docutils literal"><span class="pre">params</span></tt>
argument of the constructor:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Good</span>
<span class="n">ValidationError</span><span class="p">(</span>
    <span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value: </span><span class="si">%(value)s</span><span class="s">&#39;</span><span class="p">),</span>
    <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;value&#39;</span><span class="p">:</span> <span class="s">&#39;42&#39;</span><span class="p">},</span>
<span class="p">)</span>

<span class="c"># Bad</span>
<span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value: </span><span class="si">%s</span><span class="s">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Use mapping keys instead of positional formatting. This enables putting
the variables in any order or omitting them altogether when rewriting the
message:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Good</span>
<span class="n">ValidationError</span><span class="p">(</span>
    <span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value: </span><span class="si">%(value)s</span><span class="s">&#39;</span><span class="p">),</span>
    <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;value&#39;</span><span class="p">:</span> <span class="s">&#39;42&#39;</span><span class="p">},</span>
<span class="p">)</span>

<span class="c"># Bad</span>
<span class="n">ValidationError</span><span class="p">(</span>
    <span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value: </span><span class="si">%s</span><span class="s">&#39;</span><span class="p">),</span>
    <span class="n">params</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;42&#39;</span><span class="p">,),</span>
<span class="p">)</span>
</pre></div>
</div>
</li>
<li><p class="first">Wrap the message with <tt class="docutils literal"><span class="pre">gettext</span></tt> to enable translation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Good</span>
<span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value&#39;</span><span class="p">))</span>

<span class="c"># Bad</span>
<span class="n">ValidationError</span><span class="p">(</span><span class="s">&#39;Invalid value&#39;</span><span class="p">)</span>
</pre></div>
</div>
</li>
</ul>
<p>Putting it all together:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
    <span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value: </span><span class="si">%(value)s</span><span class="s">&#39;</span><span class="p">),</span>
    <span class="n">code</span><span class="o">=</span><span class="s">&#39;invalid&#39;</span><span class="p">,</span>
    <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;value&#39;</span><span class="p">:</span> <span class="s">&#39;42&#39;</span><span class="p">},</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Following these guidelines is particularly necessary if you write reusable
forms, form fields, and model fields.</p>
<p>While not recommended, if you are at the end of the validation chain
(i.e. your form <tt class="docutils literal"><span class="pre">clean()</span></tt> method) and you know you will <em>never</em> need
to override your error message you can still opt for the less verbose:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Invalid value: </span><span class="si">%s</span><span class="s">&#39;</span><span class="p">)</span> <span class="o">%</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<span class="title">New in Django 1.7.</span> </div>
<p>The <a class="reference internal" href="api.html#django.forms.Form.errors.as_data" title="django.forms.Form.errors.as_data"><tt class="xref py py-meth docutils literal"><span class="pre">Form.errors.as_data()</span></tt></a> and
<a class="reference internal" href="api.html#django.forms.Form.errors.as_json" title="django.forms.Form.errors.as_json"><tt class="xref py py-meth docutils literal"><span class="pre">Form.errors.as_json()</span></tt></a> methods
greatly benefit from fully featured <tt class="docutils literal"><span class="pre">ValidationError</span></tt>s (with a <tt class="docutils literal"><span class="pre">code</span></tt> name
and a <tt class="docutils literal"><span class="pre">params</span></tt> dictionary).</p>
<div class="section" id="s-raising-multiple-errors">
<span id="raising-multiple-errors"></span><h3>Raising multiple errors<a class="headerlink" href="#raising-multiple-errors" title="Permalink to this headline">¶</a></h3>
<p>If you detect multiple errors during a cleaning method and wish to signal all
of them to the form submitter, it is possible to pass a list of errors to the
<tt class="docutils literal"><span class="pre">ValidationError</span></tt> constructor.</p>
<p>As above, it is recommended to pass a list of <tt class="docutils literal"><span class="pre">ValidationError</span></tt> instances
with <tt class="docutils literal"><span class="pre">code</span></tt>s and <tt class="docutils literal"><span class="pre">params</span></tt> but a list of strings will also work:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Good</span>
<span class="k">raise</span> <span class="n">ValidationError</span><span class="p">([</span>
    <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Error 1&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s">&#39;error1&#39;</span><span class="p">),</span>
    <span class="n">ValidationError</span><span class="p">(</span><span class="n">_</span><span class="p">(</span><span class="s">&#39;Error 2&#39;</span><span class="p">),</span> <span class="n">code</span><span class="o">=</span><span class="s">&#39;error2&#39;</span><span class="p">),</span>
<span class="p">])</span>

<span class="c"># Bad</span>
<span class="k">raise</span> <span class="n">ValidationError</span><span class="p">([</span>
    <span class="n">_</span><span class="p">(</span><span class="s">&#39;Error 1&#39;</span><span class="p">),</span>
    <span class="n">_</span><span class="p">(</span><span class="s">&#39;Error 2&#39;</span><span class="p">),</span>
<span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="s-using-validation-in-practice">
<span id="using-validation-in-practice"></span><h2>Using validation in practice<a class="headerlink" href="#using-validation-in-practice" title="Permalink to this headline">¶</a></h2>
<p>The previous sections explained how validation works in general for forms.
Since it can sometimes be easier to put things into place by seeing each
feature in use, here are a series of small examples that use each of the
previous features.</p>
<div class="section" id="s-using-validators">
<span id="s-validators"></span><span id="using-validators"></span><span id="validators"></span><h3>Using validators<a class="headerlink" href="#using-validators" title="Permalink to this headline">¶</a></h3>
<p>Django&#8217;s form (and model) fields support use of simple utility functions and
classes known as validators. A validator is merely a callable object or
function that takes a value and simply returns nothing if the value is valid or
raises a <a class="reference internal" href="../exceptions.html#django.core.exceptions.ValidationError" title="django.core.exceptions.ValidationError"><tt class="xref py py-exc docutils literal"><span class="pre">ValidationError</span></tt></a> if not. These can be
passed to a field&#8217;s constructor, via the field&#8217;s <tt class="docutils literal"><span class="pre">validators</span></tt> argument, or
defined on the <a class="reference internal" href="fields.html#django.forms.Field" title="django.forms.Field"><tt class="xref py py-class docutils literal"><span class="pre">Field</span></tt></a> class itself with the
<tt class="docutils literal"><span class="pre">default_validators</span></tt> attribute.</p>
<p>Simple validators can be used to validate values inside the field, let&#8217;s have
a look at Django&#8217;s <tt class="docutils literal"><span class="pre">SlugField</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.forms</span> <span class="kn">import</span> <span class="n">CharField</span>
<span class="kn">from</span> <span class="nn">django.core</span> <span class="kn">import</span> <span class="n">validators</span>

<span class="k">class</span> <span class="nc">SlugField</span><span class="p">(</span><span class="n">CharField</span><span class="p">):</span>
    <span class="n">default_validators</span> <span class="o">=</span> <span class="p">[</span><span class="n">validators</span><span class="o">.</span><span class="n">validate_slug</span><span class="p">]</span>
</pre></div>
</div>
<p>As you can see, <tt class="docutils literal"><span class="pre">SlugField</span></tt> is just a <tt class="docutils literal"><span class="pre">CharField</span></tt> with a customized
validator that validates that submitted text obeys to some character rules.
This can also be done on field definition so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">slug</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">SlugField</span><span class="p">()</span>
</pre></div>
</div>
<p>is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">slug</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">validators</span><span class="o">=</span><span class="p">[</span><span class="n">validators</span><span class="o">.</span><span class="n">validate_slug</span><span class="p">])</span>
</pre></div>
</div>
<p>Common cases such as validating against an email or a regular expression can be
handled using existing validator classes available in Django. For example,
<tt class="docutils literal"><span class="pre">validators.validate_slug</span></tt> is an instance of
a <a class="reference internal" href="../validators.html#django.core.validators.RegexValidator" title="django.core.validators.RegexValidator"><tt class="xref py py-class docutils literal"><span class="pre">RegexValidator</span></tt></a> constructed with the first
argument being the pattern: <tt class="docutils literal"><span class="pre">^[-a-zA-Z0-9_]+$</span></tt>. See the section on
<a class="reference internal" href="../validators.html"><em>writing validators</em></a> to see a list of what is already
available and for an example of how to write a validator.</p>
</div>
<div class="section" id="s-form-field-default-cleaning">
<span id="form-field-default-cleaning"></span><h3>Form field default cleaning<a class="headerlink" href="#form-field-default-cleaning" title="Permalink to this headline">¶</a></h3>
<p>Let&#8217;s first create a custom form field that validates its input is a string
containing comma-separated email addresses. The full class looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>
<span class="kn">from</span> <span class="nn">django.core.validators</span> <span class="kn">import</span> <span class="n">validate_email</span>

<span class="k">class</span> <span class="nc">MultiEmailField</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Field</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="s">&quot;Normalize data to a list of strings.&quot;</span>

        <span class="c"># Return an empty list if no input was given.</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">[]</span>
        <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;,&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="s">&quot;Check if value consists only of valid emails.&quot;</span>

        <span class="c"># Use the parent&#39;s handling of required fields, etc.</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">MultiEmailField</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">email</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
            <span class="n">validate_email</span><span class="p">(</span><span class="n">email</span><span class="p">)</span>
</pre></div>
</div>
<p>Every form that uses this field will have these methods run before anything
else can be done with the field&#8217;s data. This is cleaning that is specific to
this type of field, regardless of how it is subsequently used.</p>
<p>Let&#8217;s create a simple <tt class="docutils literal"><span class="pre">ContactForm</span></tt> to demonstrate how you&#8217;d use this
field:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="n">subject</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">message</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">CharField</span><span class="p">()</span>
    <span class="n">sender</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">EmailField</span><span class="p">()</span>
    <span class="n">recipients</span> <span class="o">=</span> <span class="n">MultiEmailField</span><span class="p">()</span>
    <span class="n">cc_myself</span> <span class="o">=</span> <span class="n">forms</span><span class="o">.</span><span class="n">BooleanField</span><span class="p">(</span><span class="n">required</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
<p>Simply use <tt class="docutils literal"><span class="pre">MultiEmailField</span></tt> like any other form field. When the
<tt class="docutils literal"><span class="pre">is_valid()</span></tt> method is called on the form, the <tt class="docutils literal"><span class="pre">MultiEmailField.clean()</span></tt>
method will be run as part of the cleaning process and it will, in turn, call
the custom <tt class="docutils literal"><span class="pre">to_python()</span></tt> and <tt class="docutils literal"><span class="pre">validate()</span></tt> methods.</p>
</div>
<div class="section" id="s-cleaning-a-specific-field-attribute">
<span id="cleaning-a-specific-field-attribute"></span><h3>Cleaning a specific field attribute<a class="headerlink" href="#cleaning-a-specific-field-attribute" title="Permalink to this headline">¶</a></h3>
<p>Continuing on from the previous example, suppose that in our <tt class="docutils literal"><span class="pre">ContactForm</span></tt>,
we want to make sure that the <tt class="docutils literal"><span class="pre">recipients</span></tt> field always contains the address
<tt class="docutils literal"><span class="pre">&quot;fred&#64;example.com&quot;</span></tt>. This is validation that is specific to our form, so we
don&#8217;t want to put it into the general <tt class="docutils literal"><span class="pre">MultiEmailField</span></tt> class. Instead, we
write a cleaning method that operates on the <tt class="docutils literal"><span class="pre">recipients</span></tt> field, like so:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="c"># Everything as before.</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">clean_recipients</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cleaned_data</span><span class="p">[</span><span class="s">&#39;recipients&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s">&quot;fred@example.com&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">data</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="s">&quot;You have forgotten about Fred!&quot;</span><span class="p">)</span>

        <span class="c"># Always return the cleaned data, whether you have changed it or</span>
        <span class="c"># not.</span>
        <span class="k">return</span> <span class="n">data</span>
</pre></div>
</div>
</div>
<div class="section" id="s-cleaning-and-validating-fields-that-depend-on-each-other">
<span id="s-validating-fields-with-clean"></span><span id="cleaning-and-validating-fields-that-depend-on-each-other"></span><span id="validating-fields-with-clean"></span><h3>Cleaning and validating fields that depend on each other<a class="headerlink" href="#cleaning-and-validating-fields-that-depend-on-each-other" title="Permalink to this headline">¶</a></h3>
<p>Suppose we add another requirement to our contact form: if the <tt class="docutils literal"><span class="pre">cc_myself</span></tt>
field is <tt class="docutils literal"><span class="pre">True</span></tt>, the <tt class="docutils literal"><span class="pre">subject</span></tt> must contain the word <tt class="docutils literal"><span class="pre">&quot;help&quot;</span></tt>. We are
performing validation on more than one field at a time, so the form&#8217;s
<a class="reference internal" href="api.html#django.forms.Form.clean" title="django.forms.Form.clean"><tt class="xref py py-meth docutils literal"><span class="pre">clean()</span></tt></a> method is a good spot to do this. Notice that we are
talking about the <tt class="docutils literal"><span class="pre">clean()</span></tt> method on the form here, whereas earlier we were
writing a <tt class="docutils literal"><span class="pre">clean()</span></tt> method on a field. It&#8217;s important to keep the field and
form difference clear when working out where to validate things. Fields are
single data points, forms are a collection of fields.</p>
<p>By the time the form&#8217;s <tt class="docutils literal"><span class="pre">clean()</span></tt> method is called, all the individual field
clean methods will have been run (the previous two sections), so
<tt class="docutils literal"><span class="pre">self.cleaned_data</span></tt> will be populated with any data that has survived so
far. So you also need to remember to allow for the fact that the fields you
are wanting to validate might not have survived the initial individual field
checks.</p>
<p>There are two ways to report any errors from this step. Probably the most
common method is to display the error at the top of the form. To create such
an error, you can raise a <tt class="docutils literal"><span class="pre">ValidationError</span></tt> from the <tt class="docutils literal"><span class="pre">clean()</span></tt> method. For
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="c"># Everything as before.</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">cleaned_data</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ContactForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">clean</span><span class="p">()</span>
        <span class="n">cc_myself</span> <span class="o">=</span> <span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;cc_myself&quot;</span><span class="p">)</span>
        <span class="n">subject</span> <span class="o">=</span> <span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;subject&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">cc_myself</span> <span class="ow">and</span> <span class="n">subject</span><span class="p">:</span>
            <span class="c"># Only do something if both fields are valid so far.</span>
            <span class="k">if</span> <span class="s">&quot;help&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">subject</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">forms</span><span class="o">.</span><span class="n">ValidationError</span><span class="p">(</span><span class="s">&quot;Did not send for &#39;help&#39; in &quot;</span>
                        <span class="s">&quot;the subject despite CC&#39;ing yourself.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<span class="title">Changed in Django 1.7:</span> <p>In previous versions of Django, <tt class="docutils literal"><span class="pre">form.clean()</span></tt> was required to return
a dictionary of <tt class="docutils literal"><span class="pre">cleaned_data</span></tt>. This method may still return a dictionary
of data to be used, but it&#8217;s no longer required.</p>
</div>
<p>In this code, if the validation error is raised, the form will display an
error message at the top of the form (normally) describing the problem.</p>
<p>Note that the call to <tt class="docutils literal"><span class="pre">super(ContactForm,</span> <span class="pre">self).clean()</span></tt> in the example code
ensures that any validation logic in parent classes is maintained.</p>
<p>The second approach might involve assigning the error message to one of the
fields. In this case, let&#8217;s assign an error message to both the &#8220;subject&#8221; and
&#8220;cc_myself&#8221; rows in the form display. Be careful when doing this in practice,
since it can lead to confusing form output. We&#8217;re showing what is possible
here and leaving it up to you and your designers to work out what works
effectively in your particular situation. Our new code (replacing the previous
sample) looks like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">forms</span>

<span class="k">class</span> <span class="nc">ContactForm</span><span class="p">(</span><span class="n">forms</span><span class="o">.</span><span class="n">Form</span><span class="p">):</span>
    <span class="c"># Everything as before.</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">clean</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">cleaned_data</span> <span class="o">=</span> <span class="nb">super</span><span class="p">(</span><span class="n">ContactForm</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">clean</span><span class="p">()</span>
        <span class="n">cc_myself</span> <span class="o">=</span> <span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;cc_myself&quot;</span><span class="p">)</span>
        <span class="n">subject</span> <span class="o">=</span> <span class="n">cleaned_data</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;subject&quot;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">cc_myself</span> <span class="ow">and</span> <span class="n">subject</span> <span class="ow">and</span> <span class="s">&quot;help&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">subject</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s">u&quot;Must put &#39;help&#39; in subject when cc&#39;ing yourself.&quot;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_error</span><span class="p">(</span><span class="s">&#39;cc_myself&#39;</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">add_error</span><span class="p">(</span><span class="s">&#39;subject&#39;</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
</pre></div>
</div>
<p>The second argument of <tt class="docutils literal"><span class="pre">add_error()</span></tt> can be a simple string, or preferably
an instance of <tt class="docutils literal"><span class="pre">ValidationError</span></tt>. See <a class="reference internal" href="#raising-validation-error"><em>Raising ValidationError</em></a> for
more details. Note that <tt class="docutils literal"><span class="pre">add_error()</span></tt> automatically removes the field
from <tt class="docutils literal"><span class="pre">cleaned_data</span></tt>.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Form and field validation</a><ul>
<li><a class="reference internal" href="#raising-validationerror">Raising <tt class="docutils literal"><span class="pre">ValidationError</span></tt></a><ul>
<li><a class="reference internal" href="#raising-multiple-errors">Raising multiple errors</a></li>
</ul>
</li>
<li><a class="reference internal" href="#using-validation-in-practice">Using validation in practice</a><ul>
<li><a class="reference internal" href="#using-validators">Using validators</a></li>
<li><a class="reference internal" href="#form-field-default-cleaning">Form field default cleaning</a></li>
<li><a class="reference internal" href="#cleaning-a-specific-field-attribute">Cleaning a specific field attribute</a></li>
<li><a class="reference internal" href="#cleaning-and-validating-fields-that-depend-on-each-other">Cleaning and validating fields that depend on each other</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>Browse</h3>
  <ul>
    
      <li>Prev: <a href="widgets.html">Widgets</a></li>
    
    
      <li>Next: <a href="../middleware.html">Middleware</a></li>
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../index.html">API Reference</a>
        
          <ul><li><a href="index.html">Forms</a>
        
        <ul><li>Form and field validation</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../../_sources/ref/forms/validation.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="widgets.html" title="Widgets">previous</a>
     |
    <a href="../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="../middleware.html" title="Middleware">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>